import React from 'react';

interface IProps {
	children: React.ReactChild
	errorRender: React.ReactNode
}

interface IState {
	hasError: boolean;
}

export default class ErrorBoundary extends React.Component<IProps, IState> {

	constructor( props ) {
		super( props );
		this.state = { hasError: false };
	}

	componentDidCatch( error, errorInfo ) {
		this.setState( {
			hasError: true
		} );
	}

	render() {
		if ( this.state.hasError ) {
			// 你可以自定义降级后的 UI 并渲染
			return this.props.errorRender;
		}

		return this.props.children;
	}
}
